// pages/foreign/foreign.js
import * as echarts from '../../ec-canvas/echarts';
import geoJson from './mapData.js';
let chart1 = null;
const app = getApp();
const app2 = getApp();
function initChart2(canvas, width, height, dpr) {
  const chart2 = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart2);

  echarts.registerMap('henan', geoJson);

  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}'
    },

    visualMap: {
      min: 0,
      max: 100,
      left: 'left',
      top: 'bottom',
      text: ['高', '低'], // 文本，默认为数值文本
      calculable: true
    },
    toolbox: {
      show: true,
      orient: 'vertical',
      left: 'right',
      top: 'center',
      feature: {
        dataView: { readOnly: false },
        restore: {},
        saveAsImage: {}
      }
    },
    series: [{
      type: 'map',
      mapType: 'henan',
      label: {
        normal: {
          show: false
        },
        emphasis: {
          textStyle: {
            color: '#fff'
          }
        }
      },
      itemStyle: {

        normal: {
          borderColor: '#389BB7',
          areaColor: '#fff',
        },
        emphasis: {
          areaColor: '#389BB7',
          borderWidth: 0
        }
      },
      animation: false,

      data: [
        { name: '郑州市', value: 100 },
        { name: '洛阳市', value: 10 },
        { name: '开封市', value: 20 },
        { name: '信阳市', value: 30 },
        { name: '驻马店市', value: 40 },
        { name: '南阳市', value: 41 },
        { name: '周口市', value: 15 },
        { name: '许昌市', value: 25 },
        { name: '平顶山市', value: 35 },
        { name: '新乡市', value: 35 },
        { name: '漯河市', value: 35 },
        { name: '商丘市', value: 35 },
        { name: '三门峡市', value: 35 },
        { name: '济源市', value: 35 },
        { name: '焦作市', value: 35 },
        { name: '安阳市', value: 35 },
        { name: '鹤壁市', value: 35 },
        { name: '濮阳市', value: 35 },
        { name: '开封市', value: 45 }
      ]

    }],

  };

  chart2.setOption(option);

  return chart2;
}
function initChart1(canvas, width, height, dpr) {
  chart1 = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart1);

  var option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      },
      confine: true
    },
    grid: {
      left: 10,
      right: 10,
      bottom: 0,
      top: 20,
      containLabel: true
    },
    yAxis: [
      {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['印度', '巴西', '阿根廷', '美国', '哥伦比亚', '法国', '伊朗','俄罗斯','土耳其','马来西亚'],
        axisLine: {
          lineStyle: {
            color: '#999'
          }
        },
        axisLabel: {
          color: '#666'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          normal: {
            show: false,
            position: 'inside'
          }
        },
        data: [179770,79459,35399,24843,23487,12646,9994,9039,8738,7857],
        itemStyle: {
          // emphasis: {
          //   color: '#37a2da'
          // }
        }
      },
    ]
  };

  chart1.setOption(option);
  return chart1;
}
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: innerWidth,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '',
      left: 'center'
    },
    grid: {
      left: 10,
      right: 10,
      bottom: 0,
      top: 20,
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: ['','3.11', '4.6', '5.2', '5.28', '6.23', '7.19', '8.14','9.9','10.5','10.31','11.26','12.22','1.17','2.12','3.10','4.5','5.1','5.27'],
      axisLabel:{
        rotate:19,
      }
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: 'A',
      type: 'line',
      smooth: true,
      data: [12,4105,97905,69612,118052,176112,252651,226290,204880,324952,462073,519233,600241,817156,677416,597246,619460,797504,566620]
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({
  /**
   * 页面的初始数据
   */
  onShareAppMessage: function (res) {
    return {
      title: 'ECharts 可以在微信小程序中使用啦！',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },
  data: {
    countryName:'',
    time:'',
    currentNum1:0,
    currentNum2:0,
    currentNum3:0,
    num1:'',
    num2:'',
    num3:'',
    num4:'',
    rate1:'',
    rate2:'',
    numbers:'',
    numbers1:'',
    ec: {
      onInit: initChart
    },
    ec1: {
      onInit: initChart1
    },
      ec2: {
        onInit: initChart2
      }
  },
      
  changeMsg(){
    console.log(this);
    this.setData({

    })
  },
  changeNum1(event){
    var num=event.currentTarget.dataset.id
    this.setData({
      currentNum1:num
    })
  },
  changeNum2(event){
    var num=event.currentTarget.dataset.id
    this.setData({
      currentNum2:num
    })
  },
  changeNum3(event){
    var num=event.currentTarget.dataset.id
    this.setData({
      currentNum3:num
    })
  },

  gotoDetail:function(event){
    var name=event.currentTarget.dataset.name
    wx.navigateTo({
      url: '../home/home?name='+name,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    var date=new Date()
    var month=date.getMonth()+1
    var time1=date.getFullYear()+'.'+month+'.'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()
    var _this=this
    wx.request({
      url: 'https://lab.isaaclin.cn/nCoV/api/area?latest=1',
      success(res){
        console.log(res);
        console.log(_this);
        _this.setData({
          countryName:res.data.results,
          time:time1
        })
      }
    }),
    wx.request({
      url: 'http://api.yonyoucloud.com/apis/dst/ncov/wholeworld',
      header: {'authoration':'apicode','apicode':'df74de29016747489d54190dbfd907b2'},
      success(res){
        console.log(res);
        console.log(_this);
        _this.setData({
          numbers:res.data.data.continent[5],
          numbers1:(res.data.data.continent[5].confirmedCount)+(res.data.data.continent[5].curedCount)+(res.data.data.continent[5].deadCount),
          num1:res.data.data.curedCount-res.data.data.continent[0].country[29].curedCount,
          num2:res.data.data.confirmedCount-res.data.data.continent[0].country[29].confirmedCount,
          num3:res.data.data.deadCount-res.data.data.continent[0].country[29].deadCount,
          num4:(res.data.data.confirmedCount-res.data.data.continent[0].country[29].confirmedCount)-(res.data.data.deadCount-res.data.data.continent[0].country[29].deadCount)-(res.data.data.curedCount-res.data.data.continent[0].country[29].curedCount),
          rate1:(100*(res.data.data.curedCount-res.data.data.continent[0].country[29].curedCount)/(res.data.data.confirmedCount-res.data.data.continent[0].country[29].confirmedCount)).toFixed(2),
          rate2:(100*(res.data.data.deadCount-res.data.data.continent[0].country[29].deadCount)/(res.data.data.confirmedCount-res.data.data.continent[0].country[29].confirmedCount)).toFixed(2)
        })
      }
    })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(function () {
      // 获取 chart 实例的方式
      // console.log(chart)
    }, 2000);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})